package com.chezhujie.www.controller;

import com.chezhujie.www.domain.Czjbusiness;
import com.chezhujie.www.service.CzjbusinessService;
import com.chezhujie.www.service.CzjstatisticalService;
import com.sys.core.page.Pagination;
import com.sys.user.domain.Xyuser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 统计
 * Created by 汤伟杰 on 2017/8/4.
 */
@Controller
@RequestMapping("/czjstatistical")
public class CzjstatisticalController {
    @Autowired
    private CzjstatisticalService czjstatisticalService;
    @Autowired
    private CzjbusinessService czjbusinessService;

    /**
     * 商品订单统计
     * @param pageNo
     * @param pageSize
     * @param createtimeStart
     * @param createtimeEnd
     * @param businessName
     * @param orderType
     * @param req
     * @param resp
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping("/orderlist.do")
    public String orderlist(Integer pageNo, Integer pageSize,
                       Date createtimeStart, Date createtimeEnd, String businessName,Integer orderType,
                       HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model) throws Exception {
        //查询条件保持传输
        pageNo = pageNo==null?pageNo=1:pageNo;
        pageSize = pageSize==null?pageSize=20:pageSize;
        model.addAttribute("pageNo", pageNo);
        model.addAttribute("pageSize", pageSize);
        model.addAttribute("createtimeStart", createtimeStart);
        model.addAttribute("createtimeEnd", createtimeEnd);
        model.addAttribute("businessName", businessName);
        model.addAttribute("orderType", orderType);

        Map<String, Object> param = new HashMap<String, Object>();
        param.put("createtimeStart",createtimeStart);
        param.put("createtimeEnd",createtimeEnd);
        param.put("businessName",businessName);
        param.put("orderType",orderType);
        //查询统计，统计商家订单状态，数量，金额
        Pagination pagination= czjstatisticalService.orderList(pageNo,pageSize,param,model);
        BigDecimal totalAmount = czjstatisticalService.getTotalAmountByOrder(param);
        model.addAttribute("pagination", pagination);
        model.addAttribute("totalAmount", totalAmount);
        return "statistical/orderlist";
    }

    /**
     * 活动订单统计
     * @param pageNo
     * @param pageSize
     * @param createtimeStart
     * @param createtimeEnd
     * @param actName
     * @param orderType
     * @param req
     * @param resp
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping("/activitylist.do")
    public String activitylist(Integer pageNo, Integer pageSize,
                       Date createtimeStart, Date createtimeEnd, String actName,Integer orderType,
                       HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model) throws Exception {
        //查询条件保持传输
        pageNo = pageNo==null?pageNo=1:pageNo;
        pageSize = pageSize==null?pageSize=20:pageSize;
        model.addAttribute("pageNo", pageNo);
        model.addAttribute("pageSize", pageSize);
        model.addAttribute("createtimeStart", createtimeStart);
        model.addAttribute("createtimeEnd", createtimeEnd);
        model.addAttribute("actName", actName);
        model.addAttribute("orderType", orderType);

        Map<String, Object> param = new HashMap<String, Object>();
        param.put("createtimeStart",createtimeStart);
        param.put("createtimeEnd",createtimeEnd);
        param.put("actName",actName);
        param.put("orderType",orderType);
        //查询统计，统计商家订单状态，数量，金额
        Pagination pagination= czjstatisticalService.activityList(pageNo,pageSize,param,model);
        model.addAttribute("pagination", pagination);
        BigDecimal totalAmount = czjstatisticalService.getTotalAmountByActivity(param);
        model.addAttribute("totalAmount", totalAmount);
        return "statistical/activitylist";
    }

    /**
     * 商家商品订单统计
     * @param pageNo
     * @param pageSize
     * @param createtimeStart
     * @param createtimeEnd
     * @param commodityName
     * @param orderType
     * @param req
     * @param resp
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping("/commoditylist.do")
    public String commoditylist(Integer pageNo, Integer pageSize,
                            Date createtimeStart, Date createtimeEnd, String commodityName,Integer orderType,
                            HttpServletRequest req, HttpServletResponse resp,
                            ModelMap model,HttpSession httpSession) throws Exception {
        Xyuser user = (Xyuser) httpSession.getAttribute("adminuser");
        if (null == user) {

            model.addAttribute("error", "用户为空");
            return "admin/nopermissions";
        } else {
            Czjbusiness czjbusiness = czjbusinessService
                    .getBusinessByTuserid(user.getUserId());// 根据用户ID查询businessid
            if (czjbusiness == null) {
                return  "admin/nopermissions";
            } else {
                //查询条件保持传输
                pageNo = pageNo==null?pageNo=1:pageNo;
                pageSize = pageSize==null?pageSize=20:pageSize;
                model.addAttribute("pageNo", pageNo);
                model.addAttribute("pageSize", pageSize);
                model.addAttribute("createtimeStart", createtimeStart);
                model.addAttribute("createtimeEnd", createtimeEnd);
                model.addAttribute("commodityName", commodityName);
                model.addAttribute("orderType", orderType);

                Map<String, Object> param = new HashMap<String, Object>();
                param.put("createtimeStart",createtimeStart);
                param.put("createtimeEnd",createtimeEnd);
                param.put("commodityName",commodityName);
                param.put("businessId",czjbusiness.getId());
                param.put("orderType",orderType);
                //查询统计，统计商家订单状态，数量，金额
                Pagination pagination= czjstatisticalService.commodityList(pageNo,pageSize,param,model);
                model.addAttribute("pagination", pagination);
                BigDecimal totalAmount = czjstatisticalService.getTotalAmountByCommodity(param);
                model.addAttribute("totalAmount", totalAmount);
                return "statistical/commoditylist";


            }

        }



    }
}
